package com.giveu.signature.module.template.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.giveu.common.exception.ResultRuntimeException;
import com.giveu.common.page.PageModel;
import com.giveu.common.page.QueryMap;
import com.giveu.common.service.RedisService;
import com.giveu.signature.common.util.MapperUtil;
import com.giveu.signature.module.template.mapper.SignTemplateParamMapper;
import com.giveu.signature.module.template.model.SignTemplateHtml;
import com.giveu.signature.module.template.model.SignTemplateParam;
import com.giveu.signature.module.template.service.SignTemplateParamService;
@Service
public class SignTemplateParamServiceImpl implements SignTemplateParamService	 {
	
	private static final Logger log = LoggerFactory.getLogger(SignTemplateParamServiceImpl.class);
	
	@Autowired
	RedisService redisService;
	@Autowired
	SignTemplateParamMapper signTemplateParamMapper;

	@Override
	public SignTemplateParam get(Long id) {
		return signTemplateParamMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo<SignTemplateParam> listByPage(QueryMap<SignTemplateParam> queryMap) {
		PageModel page = queryMap.getPage();
        PageHelper.startPage(page);
        List<SignTemplateParam> list = new ArrayList<>();
        try {
            list = signTemplateParamMapper.selectList(queryMap.getEntity());
        } catch (Exception e) {
        	throw new ResultRuntimeException("数据查询异常",e);
        }
        return new PageInfo<>(list);
	}

	@Override
	public void save(SignTemplateParam entity) {
		checkTemplate(entity);
		if(entity.getId()==null){
			signTemplateParamMapper.insertSelective(entity);
		}else{
			signTemplateParamMapper.updateByPrimaryKeySelective(entity);
		}
	}
	
	private void checkTemplate(SignTemplateParam entity) {
		if(!entity.isModify()){
			SignTemplateParam record = new SignTemplateParam();
			record.setParamCode(entity.getParamCode());
			record.setParamVersion(entity.getParamVersion()==null?1:entity.getParamVersion());
			SignTemplateParam res = MapperUtil.get(signTemplateParamMapper, record);
			if(res!=null){
				if(res.getId()!=entity.getId()){
					throw new RuntimeException("参数编码存在重复");
				}
			}
		}
	}

	@Override
	public Map<String, Map<Long, String>> paramMaps() {
		SignTemplateParam record = new SignTemplateParam();
		record.setParamStatus(1);
		List<SignTemplateParam> list = signTemplateParamMapper.select(record);
		Map<String, Map<Long, String>> map = new HashMap<>();
		if(list==null||list.isEmpty()){
			return map;
		}
		Set<String> set = list.stream().map(SignTemplateParam::getHttpSysName).collect(Collectors.toSet());
		for (String s : set) {
			List<SignTemplateParam> subList = list.stream().filter(e->s!=null&&s.equals(e.getHttpSysName())).collect(Collectors.toList());
			Map<Long, String> subMap = new HashMap<>();
			for (SignTemplateParam tp : subList) {
				subMap.put(tp.getId(), tp.getParamName()+"["+tp.getParamCode()+"_V"+tp.getParamVersion()+"]");
			}
			map.put(s, subMap);
		}
		return map;
	}

	@Override
	public void delete(SignTemplateParam entity) {
		signTemplateParamMapper.deleteByPrimaryKey(entity.getId());
	}
	
}
